package com.wcity.manage.web;

import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.Act;
import com.wcity.manage.service.ActService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限管理控制层
 *
 * @author dy
 */
@Controller
public class ActionAct {

    /**
     * 权限列表
     *
     * @param act
     * @param pageModel
     * @param request
     * @return
     */
    @RequestMapping( "act_list" )
    public String actList(Act act, PageModel pageModel, HttpServletRequest request ) {
        pageModel.setParams( act );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "act", act );
        request.setAttribute( "list", actService.getActListPage( pageModel ) );
        return "manage/actlist";
    }

    /**
     * 权限列表(查找带回)
     *
     * @param act
     * @param pageModel
     * @param request
     * @return
     */
    @RequestMapping( "actlookup" )
    public String actlookup( Act act, PageModel pageModel, HttpServletRequest request ) {
        pageModel.setParams( act );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "act", act );
        request.setAttribute( "list", actService.getActListPage( pageModel ) );
        return "manage/actlookup";
    }

    /**
     * 跳转至添加权限页面
     *
     * @return
     */
    @RequestMapping( "to_act_add" )
    public String toActAdd() {
        return "manage/actadd";
    }

    /**
     * 添加权限
     *
     * @param act
     */
    @RequestMapping( "act_add" )
    public void addAct( Act act, HttpServletRequest request, HttpServletResponse response ) {
        String parentId = (String) request.getParameter( "act.actId" );
        String errorMsg = actService.addAct( act, parentId );
        if ( StringUtil.isBlank(errorMsg) ) {
            // 添加成功
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "添加成功", "act_list", "", "closeCurrent", "" ) );
        } else {
            // 添加失败
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }
    }

    /**
     * 跳转至编辑权限页面
     *
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping( "to_act_edit" )
    public String toActEdit( int id, HttpServletRequest request, HttpServletResponse response ) {
        Act act = actService.getAct( id );
        if ( act == null ) {
            return "manage/actedit";
        }
        act.setId( id );
        request.setAttribute( "act", act );
        return "manage/actedit";
    }

    /**
     * 编辑用户
     *
     * @param id
     * @param act
     * @param request
     * @param response
     */
    @RequestMapping( "act_edit" )
    public void actEdit( int id, Act act, HttpServletRequest request, HttpServletResponse response ) {
        String parentId = (String) request.getParameter( "act.actId" );
        String errorMsg = actService.editAct( act, id, parentId );
        if ( StringUtil.isBlank(errorMsg) ) {
            // 添加成功
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "修改成功", "act_list", "", "closeCurrent", "" ) );
        } else {
            // 添加失败
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson("300", errorMsg, "", "", "", "") );
        }
    }

    @Resource private ActService actService;
}
